package api

import (
	"gateway/basic/config"
	__ "gateway/basic/proto"
	"gateway/handler/request"
	"gateway/pkg"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func SendSms(c *gin.Context) {
	var param request.SendSms
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	_, err := config.Usercline.SendSms(c, &__.SendSmsReq{
		Phone: param.Phone,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 400,
		"mes":  "短信发送成功",
	})

}
func Login(c *gin.Context) {
	var param request.Login
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	r, err := config.Usercline.GetPhoneByPhone(c, &__.GetPhoneByPhoneReq{
		Phone: param.Phone,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}
	if r.UserId == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "用户不存在",
		})
		return
	}
	if r.Password != param.Password {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "密码错误",
		})
		return
	}
	token, err := pkg.NewJWT("user").CreateToken(pkg.CustomClaims{
		ID:             uint(r.UserId),
		NickName:       "user",
		StandardClaims: jwt.StandardClaims{},
	})
	log.Println(token)
	c.JSON(http.StatusOK, gin.H{
		"code": 400,
		"mes":  "登陆成功",
		"data": token,
	})
}

func TypeList(c *gin.Context) {
	var param request.TypeList
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	r, err := config.Usercline.TypeList(c, &__.TypeListReq{
		Page:     param.Page,
		PageSize: param.PageSize,
		TypeName: param.TypeName,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "分类列表展示成功",
		"data": r,
	})
}

func GoodDetail(c *gin.Context) {
	var param request.GoodDeatil
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	r, err := config.Usercline.GoodDetail(c, &__.GoodDetailReq{
		Id: param.Id,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 400,
		"mes":  "商品详情展示成功",
		"data": r,
	})
}

func CommentAdd(c *gin.Context) {
	var param request.CommentCreate
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	r, err := config.Usercline.CommentCreate(c, &__.CommentCreateReq{
		UserId:  param.UserId,
		ZhuBoId: param.ZhuBoId,
		GoodId:  param.GoodId,
		Comment: param.Comment,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 400,
		"mes":  "评论成功",
		"data": r,
	})
}
